﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.Scenes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GuiLibrary.Components;

namespace GuiLibrary.layouts
{
    public class GridLayout : Layout
    {
        public int rows { get; set; }
        public int cols { get; set; }

        public GridLayout(Panel parentPanel, int[] margins, int rows, int cols, IOnElementSelected onElementSelectedListener, IOnCancel onCancelListener, IOnElementPointed onComponentPointed) :
            base(parentPanel, margins, onElementSelectedListener, onCancelListener, onComponentPointed)
        {
            this.rows = rows;
            this.cols = cols;
            //this.components = new List<Component>(components.Count);
            //foreach (Element component in components)
            //    this.components.Add(new Text(font, element.name, color, GameEngine.animations.OriginType.CENTER));
        }
        public override bool canMove(Layout.Cursor cursor)
        {
            switch (cursor)
            {
                case Cursor.DOWN:
                    if (pointedElement + cols >= getComponents().Count) return false;
                    else return true;
                case Cursor.UP:
                    if (pointedElement - cols < 0) return false;
                    else return true;
                case Cursor.LEFT:
                    if (pointedElement - 1 < 0) return false;
                    else return true;
                case Cursor.RIGHT:
                    if (pointedElement + 1 >= getComponents().Count) return false;
                    else return true;
            }
            return true;
        }
        public override void moveCursor(Cursor cursor)
        {
            switch (cursor)
            {
                case Cursor.DOWN:
                    changePointedComponent(pointedElement + cols);
                    break;
                case Cursor.UP:
                    changePointedComponent(pointedElement - cols);
                    break;
                case Cursor.LEFT:
                    changePointedComponent(pointedElement - 1);
                    break;
                case Cursor.RIGHT:
                    changePointedComponent(pointedElement + 1);
                    break;
            }
            base.moveCursor(cursor);
        }

        public override void refresh()
        {
            Vector2 panelSize = parentPanel.getSize();
            int cellWidth = (int)(panelSize.X / cols);
            int cellHeight = (int)(panelSize.Y / rows);
            int i = 0, row = 0, col = 0;
            foreach (Component component in getComponents())
            {
                row = i / cols;
                col = i % cols;
                component.relativePosition = new Vector2(col * cellWidth, row * cellHeight)
                    + new Vector2(marginLeft, marginTop);   //considering margins;
                component.refresh();

                i++;
            }
            refreshPointedComponent();
        }
        public override Vector2 getComponentSize(Component component)
        {
            Vector2 panelSize = parentPanel.getSize();
            List<Component> components = getComponents();
            return new Vector2(panelSize.X / cols, panelSize.Y / rows);
        }

        public override void eraseComponent(int componentIndex)
        {
            //nothing special to do
        }
    }
}
